﻿using System;
using System.Collections.Generic;
using AbstractBugTracker;
using SmartAssembly;

namespace Engine.Buggets
{
    internal sealed class PropertyFiltersFilterer<TBug, TBugDef> : FilterBuggetBase<TBug>
        where TBug : class, IBug
        where TBugDef : BugDef
    {
        private readonly IBugTracker<TBug, TBugDef> m_BugTracker;

        public PropertyFiltersFilterer(IBugTracker<TBug, TBugDef> bugTracker)
        {
            m_BugTracker = bugTracker;
        }

        protected override bool IncludePredicate(ProcessedReport processedReport, TBug bug)
        {
            IEnumerable<KeyValuePair<string, string>> filters = m_BugTracker.GetPropertyFilters(processedReport.ProductBuildRef.ProductRef);

            foreach (var filter in filters)
            {
                string key = filter.Key;
                string excluded = filter.Value;

                //TODO SARGE-240 what if the key is a noncustom property?
                string value = processedReport.Report.GetCustomProperty(key);
                if (String.Equals(excluded,value))
                {
                    return false;
                }
            }
            return true;
        }
    }
}
